import pygame


class MoveFont:
    def __init__(self, x, y, w, h, color, width=0):
        self.x = x
        self.y = y
        self.h = h
        self.w = w
        self.color = color
        self.width = width
        self.is_collided = False
        self.speedx = 0
        self.speedy = 0

    def move(self, speedx, speedy):
        self.x += speedx
        self.y += speedy

    def auto_move(self):
        self.move(self.speedx, self.speedy)

    def draw(self, surface):
        rect = pygame.Rect(self.x, self.y, self.w, self.h)
        pygame.draw.rect(surface, self.color, rect, self.width)

    def is_in(self, leftx, lefty, winw, winh):
        '''
        判断是否在指定区域中
        :param leftx: 左上角x坐标值
        :param lefty: 左上角y坐标值
        :param winw: 宽
        :param winh: 高
        :return:
        '''
        if leftx < self.x < winw - self.w and lefty < self.y < winh - self.h:
            return True
        else:
            return False

    def get_rect(self):
        return pygame.Rect(self.x, self.y, self.w, self.h)

    def is_collide(self, rect):
        return self.get_rect().colliderect(rect)


if __name__ == '__main__':
    rect1 = MoveFont(40, 0, 20, 20, (255, 0, 0), 0)
    rect2 = pygame.Rect(20, 50, 50, 50)
    pygame.init()
    screen = pygame.display.set_mode((200, 200))
    fps = pygame.time.Clock()
    while True:
        screen.fill((0, 0, 0))
        pygame.draw.rect(screen, (255, 255, 255), rect2, 5)
        rect1.move(1, 1)
        rect1.draw(screen)
        if not rect1.is_in(0,0,200, 200):
            print('out')
        if rect1.is_collide(rect2):
            print('collide')
        pygame.display.update()
        fps.tick(10)
